package com.base.caselib.presenter;

import android.app.Activity;

import com.base.caselib.bean.PreTrialBean;
import com.base.caselib.bean.TaskInfo;
import com.base.caselib.bean.lApprovalAdd;
import com.base.caselib.utils.DownLoadUtils;
import com.base.caselib.view.PreTrialView;
import com.base.commonlib.base.presenter.BasePresenter;
import com.base.commonlib.http.CallBackFroPark;
import com.base.commonlib.http.FileBean;
import com.base.commonlib.http.RealNetWorkUtil;
import com.base.commonlib.http.ResponseT;
import com.base.commonlib.http.utils.CallBackImpl;
import com.base.commonlib.utils.BitmapUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.SystemUtil;
import com.base.commonlib.utils.ToastUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

/**
 * Created by weikailiang on 2019/10/29.
 */

public class PreTrialPresenter extends BasePresenter<PreTrialView>{
    public boolean mHasSave=false;
    public PreTrialPresenter(PreTrialView view, Activity activity) {
        super(view, activity);
    }

    public void upPreTrial(List<String> sence,List<String> noticePic,List<String> inqu,String cFormId,String mCaseId){
        //保存
        mView.showPb_mes("正在压缩图片。。。");
        List<String> paths = new ArrayList<>();
        for (String fils : noticePic){
            paths.add(BitmapUtil.compressImage(fils));
        }
        List<String> paths2 = new ArrayList<>();
        for (String fils : inqu){
            paths2.add(BitmapUtil.compressImage(fils));
        }
        List<String> paths3 = new ArrayList<>();
        for (String fils : sence){
            paths3.add(BitmapUtil.compressImage(fils));
        }
        //压缩完成
        mView.hidePb_mes();

        mView.showPb_mes("正在保存数据");
        List<FileBean> mFileList = new ArrayList<>();
        FileBean mFileBean = null;
        for (String str : paths){
            mFileBean = new FileBean("files1",str);
            mFileList.add(mFileBean);
        }
        for (String str : paths2){
            mFileBean = new FileBean("files2",str);
            mFileList.add(mFileBean);
        }
        for (String str : paths3){
            mFileBean = new FileBean("files3",str);
            mFileList.add(mFileBean);
        }
        Disposable enqueue = RealNetWorkUtil.postPics().method("api-case/punishCase/savePics").filePaths(mFileList)
                .addParams("formId",cFormId)
                .addParams("caseId",mCaseId)
                .build().enqueueForUpFiles(new CallBackFroPark<ResponseT>() {
                    @Override
                    public void onResponse(ResponseT responseT) throws Exception {
                        mView.hidePb_mes();
                        mView.showMes(responseT.getData().toString());
                        mView.returnSuccesSave();
                    }

                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                    }

                    @Override
                    public void onNext(ResponseT responseT) {

                    }
                });

        mCompositeDisposable.add(enqueue);
    }
    public void initPics(String caseId){
        //初始化图片
        mView.showPb_mes("正在初始化数据");
        Map<String,Object> params = new HashMap<>();
        params.put("caseId",caseId);

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-case/punishCase/getNIPic")
                .params(params)
                .build().enqueue(PreTrialBean.class, new CallBackImpl<PreTrialBean>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(PreTrialBean objects) {
                        mView.hidePb_mes();
                        mView.returnPreTrialBean(objects);
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    /**
     * 申请预审
     * @param preId 预定义流程id
     * @param billId 案件id
     * @param funcId  表单id
     * @param tbFlag  自定义表单表名
     * @param content  表单标题
     * @param nextCaseStatus 下个案件状态
     */
    public void BindCase(String preId,String billId,String funcId,String tbFlag,String content,String nextCaseStatus){
        mView.showPb_mes("正在提交数据");
        lApprovalAdd mLApproval = new lApprovalAdd();
        mLApproval.setAgree("1");
        mLApproval.setBillId(billId);
        mLApproval.setProId("");
        mLApproval.setBusId("");
        mLApproval.setAppContent("申请预审");
        mLApproval.setAppCount("");
        mLApproval.setApplyUserId("");
        mLApproval.setTodoSource("02300002");
        mLApproval.setPreId(preId);
        mLApproval.setFormId(funcId);
        mLApproval.setTbFlag(tbFlag);
        mLApproval.setContent(content);
        mLApproval.setIsSendSMS(RealNetWorkUtil.TYPE.equals("0")?1:0);
        Map<String,Object> map = new HashMap<>();
        map.put("status","2");

        mLApproval.setParam(map);
        mLApproval.setModelId("y7RgLtwaDqfpJr0j1gD");
        Disposable disposable = RealNetWorkUtil.postContent()
                .method("api-process/lApp/addOwn")
                .content(mLApproval)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        mView.showMes(objects);
                        mView.returnNext();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    public void getNoticePdfUrl(String mCaseId, final String name){
        //初始化图片
        mView.showPb_mes("正在请求地址");
        Map<String,Object> params = new HashMap<>();
        params.put("dataId",mCaseId);
        params.put("funcId","xianchangjiancha");
        params.put("modelId","YPijz0ywa52U8vNioqq");
        params.put("funcIdstart","");

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-model/wordworkazforxcjc")
                .params(params)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        downLoadFiel(RealNetWorkUtil.getBaseUrl()+"api-file"+objects,name);
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    public void getPdfUrlZLZG(String mCaseId, final String name){
        //初始化图片
        mView.showPb_mes("正在请求地址");
        Map<String,Object> params = new HashMap<>();
        params.put("dataId",mCaseId);
        params.put("funcId","xianchangjiancha");
        params.put("modelId","YPijz0ywa52U8vNioqw");

        Disposable disposable = RealNetWorkUtil.get()
                .method("api-model/wordworkazforxcjc")
                .params(params)
                .build().enqueue(String.class, new CallBackImpl<String>() {
                    @Override
                    public void onError(int code, String msg, String errMsg) {
                        mView.hidePb_mes();
                        mView.showMes(msg);
                    }

                    @Override
                    public void onNext(String objects) {
                        mView.hidePb_mes();
                        downLoadFiel(RealNetWorkUtil.getBaseUrl()+"api-file"+objects,name);
                    }
                });
        mCompositeDisposable.add(disposable);
    }
    DownLoadUtils downLoadUtils;
    public void downLoadFiel(String url,String name){
        String[] names = url.split("\\.");
        String fileName = name+"."+names[names.length-1];

        File file = new File(SystemUtil.getAppDir());
        if (!file.exists()){
            file.mkdirs();
        }
        File file1 = new File(SystemUtil.getAppDir()+fileName);
        if (file1.exists()){
            file1.delete();
        }
        TaskInfo info = new TaskInfo();
        info.setName(fileName);
        info.setPath(SystemUtil.getAppDir());
        info.setUrl(url);
        downLoadUtils = new DownLoadUtils(mActivity,info);
        downLoadUtils.showDownDialog();
    }

    @Override
    public void release() {
        super.release();
        if (downLoadUtils != null){
            downLoadUtils.onDestory();
        }
    }

}
